package com.firemoss.ui.blanket.calloutClasses
{
	import com.firemoss.ui.blanket.util.Orientation;
	
	import flash.display.*;
	import flash.filters.DropShadowFilter;
	import flash.geom.*;
	
	import mx.core.UIComponent;

	/**
	 *  The skin for a Callout.
	 */
	public class CalloutBorder extends UIComponent 
	{
		
		public var backgroundGradientColors : Array
		public var backgroundAlpha : Number
		public var cornerRadius : int
		
		public var callout : AbstractCallout
	
		public function CalloutBorder()
		{
			var shadow : DropShadowFilter = new DropShadowFilter()
			
			shadow.alpha = 0.5
			
			filters = [ shadow ]

			backgroundGradientColors = [0xd06d35, 0xbb4c13]
			backgroundAlpha = 0.95
			cornerRadius = 7
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var w : Number = callout.getExplicitOrMeasuredWidth()
			var h : Number = callout.getExplicitOrMeasuredHeight()
			var center : Number = w / 2
			var middle : Number = h / 2
			

			var fillType:String = GradientType.LINEAR;
	  		var colors:Array = backgroundGradientColors;
	  		var alphas:Array = [backgroundAlpha, backgroundAlpha];
	  		var ratios:Array = [0x00, 0x80];
	  		var matr:Matrix = new Matrix();
	  		matr.createGradientBox(w, h, Math.PI/2, 0, 0);
	 		var spreadMethod:String = SpreadMethod.PAD;
	 		
			var shadowAlpha:Number = 0.1;
			
			var g:Graphics = graphics;
			g.clear();  
			
			var or : Number = callout.currentOrientation
			switch ( callout.currentOrientation )
			{
				case Orientation.TOP_RIGHT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( 0, h - 10 )
					g.lineTo( Math.min( -10, -1 * callout.relativeX - 10 ), Math.max( h + 10, h + callout.relativeY + 10 ) ) 
					g.lineTo(10, h)
					g.lineTo(0, h)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, 0, cornerRadius )
					g.endFill()
					break;
				case Orientation.TOP_CENTER:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( center - 5, h )
					g.lineTo( center, Math.max( h + 10, h + callout.relativeY + 10 ) ) 
					g.lineTo( center + 5, h)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, cornerRadius, cornerRadius )
					g.endFill()
					break;
				case Orientation.TOP_LEFT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( w, h - 10 )
					g.lineTo( w + Math.max( 10, callout.relativeX + 10 ), Math.max( h + 10, h + callout.relativeY + 10 ) ) 
					g.lineTo( w - 10, h)
					g.lineTo(w, h)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, cornerRadius, 0 )
					g.endFill()
					break;
				case Orientation.MIDDLE_RIGHT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( 0, middle - 5 )
					g.lineTo( Math.min( -10, -1 * callout.relativeX - 10 ), middle )
					g.lineTo( 0, middle + 5)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, cornerRadius, cornerRadius )
					g.endFill()
					break;
				case Orientation.MIDDLE_LEFT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( w, middle - 5 )
					g.lineTo( Math.max( w + 10, w + callout.relativeX + 10 ), middle ) 
					g.lineTo( w, middle + 5)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, cornerRadius, cornerRadius )
					g.endFill()
					break;
				case Orientation.BOTTOM_RIGHT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( 10, 0 )
					g.lineTo( Math.min( -10, -1 * callout.relativeX - 10 ), Math.min( -10, -1 * callout.relativeY - 10 )  ) 
					g.lineTo( 0, 10 )
					g.lineTo(0, 0)

					g.drawRoundRectComplex(0, 0, w, h, 0, cornerRadius, cornerRadius, cornerRadius )
					g.endFill()
					break;
				case Orientation.BOTTOM_CENTER:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( center - 5, 0 )
					g.lineTo( center, Math.min( -10, -1 * callout.relativeY - 10 ) )
					g.lineTo( center + 5, 0)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, cornerRadius, cornerRadius, cornerRadius )
					g.endFill()
					break;
				case Orientation.BOTTOM_LEFT:
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod );  

					g.moveTo( w - 10, 0 )
					g.lineTo( w + Math.max( 10, callout.relativeX + 10 ), Math.min( -10, -1 * callout.relativeY - 10 )  ) 
					g.lineTo( w, 10 )
					g.lineTo( w, 0)

					g.drawRoundRectComplex(0, 0, w, h, cornerRadius, 0, cornerRadius, cornerRadius )
					g.endFill()
					break;
			}
		}
	}
}